package com.kaiyiweitong.tongxing.activity.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.GridLayout;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.adapter.MyOrderAdapter;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.fragment.my_order.BackedFragment;
import com.kaiyiweitong.tongxing.fragment.my_order.CanceledFragment;
import com.kaiyiweitong.tongxing.fragment.my_order.FinishedFragment;
import com.kaiyiweitong.tongxing.fragment.my_order.NotPayFragment;
import com.kaiyiweitong.tongxing.fragment.my_order.PayedFragment;
import com.kaiyiweitong.tongxing.other.FragmentFactory;
import com.kaiyiweitong.tongxing.other.SpaceItemDecoration;
import com.kaiyiweitong.tongxing.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/17 0017.
 */

public class MyOrderActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private String TAG = "MyOrderActivity";
    @Bind(R.id.rv_my_order)
    RecyclerView mRvMyOrder;
    @Bind(R.id.vp_my_order)
    ViewPager mVpMyOrder;

    private List<Fragment> mFragments = new ArrayList<>();
    private MyOrderAdapter mHorAdapter;
    private String[] mStringArray;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        mStringArray = getResources().getStringArray(R.array.order_status);

        mRvMyOrder.setLayoutManager(new GridLayoutManager(this, 1, GridLayout.HORIZONTAL, false));
        mHorAdapter = new MyOrderAdapter(this, mStringArray, mListener,mCurrentIndex);
        mRvMyOrder.addItemDecoration(new SpaceItemDecoration((int)CommonUtil.getItemDecoration(this,mStringArray), 2));
        mRvMyOrder.setAdapter(mHorAdapter);


        mFragments.add(FragmentFactory.getInstance(NotPayFragment.class));
        mFragments.add(FragmentFactory.getInstance(PayedFragment.class));
        mFragments.add(FragmentFactory.getInstance(FinishedFragment.class));
        mFragments.add(FragmentFactory.getInstance(CanceledFragment.class));
        mFragments.add(FragmentFactory.getInstance(BackedFragment.class));
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }
        };
        mVpMyOrder.setAdapter(adapter);
        mVpMyOrder.setOffscreenPageLimit(4);
        mVpMyOrder.addOnPageChangeListener(this);

        indicator(0);
    }

    private MyOrderAdapter.OnClickListener mListener = new MyOrderAdapter.OnClickListener() {
        @Override
        public void onClick(int position) {
            indicator(position);
        }
    };

    private int mCurrentIndex = -1;

    private synchronized void indicator(int position) {
        if (mCurrentIndex != position) {
            mVpMyOrder.setCurrentItem(position);
            mHorAdapter = new MyOrderAdapter(this, mStringArray, mListener,position);
            mRvMyOrder.setAdapter(mHorAdapter);
            mCurrentIndex = position;
        }
    }


    @OnClick(R.id.delete)
    public void onViewClicked() {
    }


    @Override
    public void onPageSelected(int position) {
        indicator(position);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}